package cn.szzsi.platform.controller.admin;

import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.model.PlateformConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.redis.Redis;

import java.util.Date;

/**
 * Created by admin on 2017/7/10.
 */
public class ConfigController extends Controller{
    private PlateformConfig plateformConfig;
    public void view(){
        setAttr("plateformConfig", PlateformConfig.dao.findById("1"));
        render(CommonUtil.TEMPLATE_PATH + "/platform/config/plateform_config_view.html");
    }

    public void  edit(){
        setAttr("plateformConfig", PlateformConfig.dao.findById("1"));
        render(CommonUtil.TEMPLATE_PATH + "/platform/config/plateform_config_add.html");
    }

    public void update(){
        plateformConfig=getModel(PlateformConfig.class);
        if(plateformConfig.getId()!=null){//更新
            if(StrKit.isBlank(plateformConfig.getSmsSignature())){
                plateformConfig.setSmsSignature("微米荟商城");
            }
            plateformConfig.setModifiDate(new Date());
            plateformConfig.update();
        }else{//保存
            plateformConfig.setId(1);
            plateformConfig.setCreateDate(new Date());
            if(StrKit.isBlank(plateformConfig.getSmsSignature())){
                plateformConfig.setSmsSignature("微米荟商城");
            }
            plateformConfig.save();
        }

        JSONObject jsonObject= JSON.parseObject(JsonKit.toJson(plateformConfig));
        Redis.use("zshop").set("plateformConfig",jsonObject);

        renderJson(Ret.ok("info","修改成功!").set("url","view"));
    }

}
